package itbour.onetouchshow.base.mvp;

import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.lsjr.utils.NetUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.Random;

import butterknife.BindView;
import itbour.onetouchshow.R;
import itbour.onetouchshow.utils.OneTouchHeader;
import itbour.onetouchshow.utils.T_;
import itbour.onetouchshow.utils.UIUtils;

/**
 * 创建人：$ gyymz1993
 * 创建时间：2017/7/21 15:14
 */
public abstract class SmartBaseRefreshFragment<V extends BaseView, T extends BasePresenterImpl<V>> extends MVPBaseFragment<V, T> implements BaseView {

    public static final int ON_REFRESH = 1;
    public static final int ON_LOAD = 2;
    private static boolean isFirstEnter = true;
    @BindView(R.id.id_back_top)
    public ImageView mImageViewRebackTop;
    public int mPageIndex = 0;
    public int pullStatus;
    @BindView(R.id.refreshLayout)
    public RefreshLayout mRefreshLayout;
    private OneTouchHeader mClassicsHeader;
    private Drawable mDrawableProgress;

    @Override
    protected int getLayoutId() {
        return getChirdLayoutId();
    }

    protected abstract int getChirdLayoutId();

    @Override
    protected void initView() {

        mImageViewRebackTop.setVisibility(View.INVISIBLE);
        int delta = new Random().nextInt(7 * 24 * 60 * 60 * 1000);
        mClassicsHeader = (OneTouchHeader) mRefreshLayout.getRefreshHeader();
        //隐藏时间
        mClassicsHeader.setEnableLastTime(false);
//        mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()-delta));
//        mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm", Locale.CHINA));
//        mClassicsHeader.setTimeFormat(new DynamicTimeFormat("更新于 %s"));
//
////        mDrawableProgress = mClassicsHeader.getProgressView().getDrawable();
//        mDrawableProgress = ((ImageView)mClassicsHeader.findViewById(ClassicsHeader.ID_IMAGE_PROGRESS)).getDrawable();
//        if (mDrawableProgress instanceof LayerDrawable) {
//            mDrawableProgress = ((LayerDrawable) mDrawableProgress).getDrawable(0);
//        }
//        if (isFirstEnter) {
//            isFirstEnter = false;
//            //触发自动刷新
//            mRefreshLayout.autoRefresh();
//        }
        // mRefreshLayout.autoRefresh();
        mRefreshLayout.getLayout().setBackgroundResource(android.R.color.transparent);
        mRefreshLayout.setPrimaryColors(0, 0xff666666);
        mRefreshLayout.setEnableAutoLoadMore(true);
//        if (Build.VERSION.SDK_INT >= 21) {
//            mDrawableProgress.setTint(0xff666666);
//        } else if (mDrawableProgress instanceof VectorDrawableCompat) {
//            ((VectorDrawableCompat) mDrawableProgress).setTint(0xff666666);
//        }
        initRecycleView();
    }

    public void initRecycleView() {

    }


    public abstract RecyclerView.Adapter getBaseRefreshAdapter();

    public abstract View getHeadView();

    public void checkNetWork() {
        if (!NetUtils.isConnected(UIUtils.getContext())) {
            T_.showCustomToast("请检查网络");
            return;
        }
    }
}
